package com.gk.courses.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gk.courses.dao.StudentDao;
import com.gk.courses.model.Course;
import com.gk.courses.model.Student;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
	 
	@Override
	public Student getStudent(int studentId) {
		return studentDao.findById(studentId);
	}

	@Override
	public void registerStudent(Student student) {
		studentDao.create(student);
	}
	
	@Override
	public void registerCompletedCourse(Course course, Student student) {
		course = courseService.getCourse(course.getCourseId());
		student = getStudent(student.getId());
		studentDao.updateCompletedCourse(course, student);
	}

	@Override
	public List<Course> getCompletedCourses(Student student) {
		student = getStudent(student.getId());
		return studentDao.getCompletedCourses(student);
	}
	
	@Override
	public List<Course> getEnrolledCourses(Student student) {
		student = getStudent(student.getId());
		return studentDao.getEnrolledCourses(student);
	}
	
    //---------------------- injections --------------------------

	protected StudentDao studentDao;
	protected CourseService courseService;

	@Autowired
	public final void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
	
	@Autowired
	public final void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

}
